﻿<%@ WebHandler Language="C#" Class="buy" %>

using System;
using System.Web;
using OneshoppingWeb;
using OneShoppingModel;
using OneShoppingBLL;
using System.Collections.Generic;
using OneShoppingUtility;
using System.Web.SessionState;

public class buy : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string str = context.Request["buydata"];
        List<buydata> list = JsonHelper.DeserializeJsonToList<buydata>(str);
        try
        {
            SystemUser sys = (SystemUser)context.Session["user"];
            if (sys == null)
            {
                context.Response.Write(UtilityJson.ResultToJson((int)(Protocol_user.NOT_LOG)));//没有登入
                return;
            }

            List<buycodes> clist = new List<buycodes>();

            int userId = ((SystemUser)(context.Session["user"])).UserId;
           
            for (int i = 0; i < list.Count; i++)
            {
                String codes = "";
                for (int j = 0; j < list[i].amount; j++)
                {
                    PurchasingList pur = new PurchasingList();
                    pur.ListId = list[i].buyid;
                    pur.UserId = userId;
                    pur.BuyTime = DateTime.Now;
                    DateTime dt = pur.BuyTime;
                    if (!UtilitySql.isOverActivityTotal("Activity", pur.ListId))
                    {
                        int code = UtilitySql.maxTradeCode("PurchasingList", "TradeCode",pur.ListId);//;string.Format("{0:HHmmssffff}", dt);
                        code += 1;
                        pur.TradeCode = Convert.ToInt32(code);
                        pur.BuyStatus = 0;
                        PurchasingListBLL.GetInstance().Insert(pur);
                        codes = codes + code + ",";
                        Activity a = ActivityBLL.GetInstance().GetEntity(r => r.ListId == pur.ListId);
                        a.Surplus = a.Surplus - 1;
                        ActivityBLL.GetInstance().Update(a);
                        if (UtilitySql.isOverActivityTotal("Activity", pur.ListId))
                        {
                            //开奖CommonUpdate
                            //  SqlUtility.GetInstance().CommonUpdate("PurchasingList", "BuyStatus=0", "ListId=1010 and TradeCode!=1000001");
                            int code1 = UtilitySql.maxTradeCode("PurchasingList", "TradeCode", pur.ListId);
                            int winCode = new Random().Next(1000000,code1+1);
                            PurchasingList pl = PurchasingListBLL.GetInstance().GetEntity(r => r.TradeCode == winCode);
                            UtilitySql.updateTables("PurchasingList", "BuyStatus=2", "ListId='" + pl.ListId + "' and TradeCode!='" + winCode + "'");
                            UtilitySql.updateTables("PurchasingList", "BuyStatus=1", "ListId='" + pl.ListId + "' and TradeCode='" + winCode + "'");
                            ActivityResult ar = new ActivityResult();
                            ar.TradeId = pl.TradeId;
                            ar.Status = 0;
                            ar.Phone = SystemUserBLL.GetInstance().GetEntity(r=>r.UserId==pl.UserId).UserPhone;
                            ActivityResultBLL.GetInstance().Insert(ar);
                            Profit pt = new Profit();
                            pt.ListId = pl.ListId;
                            Activity at=ActivityBLL.GetInstance().GetEntity(r=>r.ListId==pl.ListId);
                            at.WinningCode = winCode;
                            at.Status = 6;
                            ActivityBLL.GetInstance().Update(at);
                            pt.SellerProfit = at.Total-at.PlatformProfit;
                            pt.PlatformProfit = at.PlatformProfit;
                            pt.PlayStatus = 0;
                            ProfitBLL.GetInstance().Insert(pt);
                            break;
                        }
                    }
                    
                }
                
                buycodes b = new buycodes();
                b.buyid = list[i].buyid;
                b.acode = codes;
                clist.Add(b);
            }
            context.Response.Write(UtilityJson.ResListToJson((int)Protocol_activity.BUY_SUCCESS, clist));//购买成功
        }
        catch
        {
            context.Response.Write(UtilityJson.ResultToJson((int)(Protocol_activity.BUY_ERROR)));//购买失败
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    class buydata
    {
        public int buyid { set; get; }
        public int amount { set; get; }
    }

    class buycodes
    {
        public int buyid { set; get; }
        public string acode { set; get; }
    }
}